package org.devio.secondlevelmenu.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.devio.secondlevelmenu.R;
import org.devio.secondlevelmenu.adapter.LabelOneRecyclerViewAdapter;
import org.devio.secondlevelmenu.adapter.LabelTwoLeftRecyclerViewAdapter;
import org.devio.secondlevelmenu.bean.VipShareFirstLebalBean;
import org.devio.secondlevelmenu.bean.VipShareSecondLeftLebalBean;
import org.devio.secondlevelmenu.bean.VipShareSecondRightLebalBean;
import org.devio.secondlevelmenu.view.flowlayout.FlowLayout;
import org.devio.secondlevelmenu.view.flowlayout.TagAdapter;
import org.devio.secondlevelmenu.view.flowlayout.TagFlowLayout;
import org.jetbrains.annotations.NotNull;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by amazingZZ on 11/15/21
 */
public class VipInfoTwoLevelTabBar extends RelativeLayout implements View.OnClickListener {


    private View root;
    private PopupWindow popWindow;
    private View popRootView;
    private TextView oneLabel, twoLabel;

    // popupwindow 展示哪个标签的内容
    public static final int ONE_LEBAL = 0, TWO_LABEL = 1, NO_SELECT = -1;
    // title tab 默认文字
    private String defaultTitle = "全部业务";
    // title tab 文字
    private String[] selectTabTitle = {defaultTitle, defaultTitle};
    // 二级标签 右侧的两个 taglayout
    private static final int SECOND_LABEL_ONE = 1, SECOND_LABEL_TWO = 2;
    public static final String SELECT_ALL = "select_all";
    // 选了几级标签
    private int selectIndex = NO_SELECT;


    // 选中的 一级标签
    private VipShareFirstLebalBean selectedFirstLabelBean;
    // 二级标签
    // 选定的 二级右侧标签
    private VipShareSecondRightLebalBean selectedSecondRightLabel;


    // 当前选定的 二级 左侧标签
    private int currentSelectedSecondLeftLabelIndex = 0;
    // 上次 选定的二级左侧标签
    private int lastSelectedSecondLeftLabelIndex = 0;

    // 上次选了子标签的 二级 左侧标签index
    private int lastSelectedChildLeftLabelInedex = -1;
    // 当前选了子标签的 二级 左侧标签index
    private int currentSelectedChildLeftLabelInedex = -1;
    // 点确认后 ，仍选中的二级左侧标签
    private int confirmSelectedSecondLeftLabelIndex = 0;

    // 点确认后 仍然 选中的 二级右侧标签
    private VipShareSecondRightLebalBean confirmSelectedSecondRightLabel;


    // 一级标签 recyclerview adapter
    private LabelOneRecyclerViewAdapter labelOneRecyclerViewAdapter;
    // 二级标签  左侧recyclerview adapter
    private LabelTwoLeftRecyclerViewAdapter labelTwoLeftRecyclerViewAdapter;
    // 二级标签 右侧 ，tagLayout adapter
    private TagAdapter tagAdapter1, tagAdapter2;

    // 二级标签  右侧 上下两个label
    private TextView rightLabel1, rightLabel2;
    // 二级标签 全部按钮
    private TextView btnAll;
    // 二级标签 重置按钮
    private TextView reset;
    // 二级标签  确认按钮
    private TextView confirm;


    //数据源
    // 一级
    private List<VipShareFirstLebalBean> shareFirstLebalBeans;
    // 二级 左
    private List<VipShareSecondLeftLebalBean> secondLeftLebalBeans;
    // 二级 右 上
    private List<VipShareSecondRightLebalBean> secondRightLebalBeans1;
    // 二级 右 下
    private List<VipShareSecondRightLebalBean> secondRightLebalBeans2;
    // 二级 右 label
    private String rightLabelStr1 = "", rightLabelStr2 = "";

    //二级 tagLayout
    private TagFlowLayout mFlowLayout;
    private TagFlowLayout mFlowLayout2;
    // 选定 item的监听器
    private ChooseLebalListener chooseLebalListener;
    // 显示 哪个顶部Tag
    private int showTopTags = NO_SELECT;

    // 重置状态
    private boolean resetStatus = false;
    // 重置状态下当前所选择的 左侧标签 index
    private int selectedSecondLeftLabelIndexInResetStatus = NO_SELECT;

    public VipInfoTwoLevelTabBar(Context context) {
        super(context);
        init();
    }


    public VipInfoTwoLevelTabBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();

    }

    private void init() {
        root = LayoutInflater.from(getContext()).inflate(R.layout.vipinfo_two_level_tabbar, this, true);
        oneLabel = root.findViewById(R.id.one_label);
        twoLabel = root.findViewById(R.id.two_label);
        oneLabel.setOnClickListener(this);
        twoLabel.setOnClickListener(this);
        setTabTitle();
    }

    // 显示哪个TAG
    public void setShowTopTags(int showTopTags) {
        this.showTopTags = showTopTags;
        if (oneLabel != null && twoLabel != null) {
            if (showTopTags == ONE_LEBAL) {
                oneLabel.setVisibility(VISIBLE);
                twoLabel.setVisibility(GONE);
            } else if (showTopTags == TWO_LABEL) {
                oneLabel.setVisibility(GONE);
                twoLabel.setVisibility(VISIBLE);
            } else {
                oneLabel.setVisibility(VISIBLE);
                twoLabel.setVisibility(VISIBLE);
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (v == oneLabel) {
            initPopUpWindow(v, ONE_LEBAL);
        } else if (v == twoLabel) {
            initPopUpWindow(v, TWO_LABEL);

        } else if (v == btnAll) {

            if (btnAll.getTag() == null || btnAll.getTag().equals(false)) {

                selectedSecondRightLabel = new VipShareSecondRightLebalBean();
                selectedSecondRightLabel.setTxt(SELECT_ALL);
                selectedSecondRightLabel.setUniId(currentSelectedSecondLeftLabelIndex + SELECT_ALL);
                btnAllUI(true);
                if (tagAdapter1 != null) tagAdapter1.clearSelectedList();
                if (tagAdapter2 != null) tagAdapter2.clearSelectedList();
                refreshSecondLeftChildSelectData(currentSelectedSecondLeftLabelIndex);

            } else {
                btnAllUI(false);
                refreshSecondLeftChildSelectData(-1);
                selectedSecondRightLabel = null;
            }


        } else if (v == reset) {
            resetStatus = true;
            resetData();
            setResetState();
            setTabTitle();
        } else if (v == confirm) {
            this.confirmSelectedSecondLeftLabelIndex = currentSelectedSecondLeftLabelIndex;
            if (chooseLebalListener != null) {
                if (resetStatus) {
                    setResetState();


                } else {
                    selectedSecondLeftLabelIndexInResetStatus = NO_SELECT;

                    if (currentSelectedSecondLeftLabelIndex == currentSelectedChildLeftLabelInedex) {
                        this.confirmSelectedSecondRightLabel = selectedSecondRightLabel;
                        if (selectedSecondRightLabel.getTxt() != null) {
                            if (selectedSecondRightLabel.getTxt().equals(SELECT_ALL)) {
                                selectTabTitle[1] = secondLeftLebalBeans.get(currentSelectedSecondLeftLabelIndex).getTxt();

                            } else {
                                selectTabTitle[1] = selectedSecondRightLabel.getTxt();
                            }
                        }
                    } else {
                        confirmSelectedSecondRightLabel = null;
                        if (secondLeftLebalBeans.get(currentSelectedSecondLeftLabelIndex) != null)
                            selectTabTitle[1] = secondLeftLebalBeans.get(currentSelectedSecondLeftLabelIndex).getTxt();

                    }

                    chooseLebalListener
                            .confirmSecondRightLabels(
                                    secondLeftLebalBeans.get(currentSelectedSecondLeftLabelIndex),
                                    confirmSelectedSecondRightLabel
                            );
                }

            }
            if (popWindow != null) {
                popWindow.dismiss();
            }

        }
    }

    private void setResetState() {
        selectTabTitle[1] = defaultTitle;

        confirmSelectedSecondLeftLabelIndex = 0;
        confirmSelectedSecondRightLabel = null;
        if (chooseLebalListener != null)
            chooseLebalListener
                    .confirmSecondRightLabels(null, null);
    }

    private void resetData() {
        if (resetStatus) {
            if (tagAdapter1 != null) tagAdapter1.clearSelectedList();
            if (tagAdapter2 != null) tagAdapter2.clearSelectedList();
            selectedSecondLeftLabelIndexInResetStatus = currentSelectedSecondLeftLabelIndex;
        } else {
            if (tagAdapter1 != null) tagAdapter1.clearDatas();
            if (tagAdapter2 != null) tagAdapter2.clearDatas();
        }

        selectedSecondRightLabel = null;

        btnAllUI(false);
        // reset child select
        if (currentSelectedChildLeftLabelInedex != -1 && currentSelectedChildLeftLabelInedex < secondLeftLebalBeans.size())
            secondLeftLebalBeans.get(currentSelectedChildLeftLabelInedex).setChildSelected(false);
        if (lastSelectedChildLeftLabelInedex != -1 && lastSelectedChildLeftLabelInedex < secondLeftLebalBeans.size())
            secondLeftLebalBeans.get(lastSelectedChildLeftLabelInedex).setChildSelected(false);
        // 上次选了子标签的 二级 左侧标签index
        lastSelectedChildLeftLabelInedex = -1;
        // 当前选了子标签的 二级 左侧标签index
        currentSelectedChildLeftLabelInedex = -1;


        if (currentSelectedSecondLeftLabelIndex != -1 && currentSelectedSecondLeftLabelIndex < secondLeftLebalBeans.size())
            secondLeftLebalBeans.get(currentSelectedSecondLeftLabelIndex).setSelected(false);
        if (!resetStatus)
            currentSelectedSecondLeftLabelIndex = 0;

//        if (secondLeftLebalBeans.get(currentSelectedSecondLeftLabelIndex) != null)
//            secondLeftLebalBeans.get(currentSelectedSecondLeftLabelIndex).setSelected(true);

        // 当前选定的 二级 左侧标签
        // 上次 选定的二级左侧标签
        lastSelectedSecondLeftLabelIndex = 0;
        setSecondLeftLabelData();
    }


    private void initPopUpWindow(View v, int selectIndex) {

        if (selectIndex == this.selectIndex) {
            return;
        }
        this.selectIndex = selectIndex;

        if (popWindow != null && popWindow.isShowing() && popRootView != null) {
            resovleShowUI(popRootView);
            return;
        }

        popRootView = LayoutInflater.from(getContext()).inflate(R.layout.vipshare_menu_pop, null, false);
        loadConfirmData();

        // 处理ui的 method
        resovleShowUI(popRootView);

        View mask = popRootView.findViewById(R.id.mask);


        popWindow = new PopupWindow(popRootView,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popWindow.setAnimationStyle(R.style.spinnerPopAnimation);
        popWindow.setFocusable(false);
        popWindow.setTouchable(true);
        popWindow.setOutsideTouchable(false);

        popWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;

            }
        });
        popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要为popWindow设置一个背景才有效
        //show popwindow
        popWindow.showAsDropDown(this, 0, 0);

        popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                VipInfoTwoLevelTabBar.this.selectIndex = NO_SELECT;
                resetStatus = false;
                setTabTitle();
                resetData();
            }
        });
        // 底部蒙层
        mask.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (popWindow != null) popWindow.dismiss();
                return false;
            }
        });

    }

    // 设置 最顶部 tab 的文字
    private void setTabTitle() {
        oneLabel.setText(selectTabTitle[0]);
        Log.e("zz", "length" + selectTabTitle[1].length());
        if (selectTabTitle[1] != null && selectTabTitle[1].length() > 8) {
            twoLabel.setText(selectTabTitle[1].substring(0, 8) + "...");

        } else {
            twoLabel.setText(selectTabTitle[1]);
        }


        if (selectIndex == ONE_LEBAL) {
            oneLabel.setTextColor(getResources().getColor(R.color.colorPrimary));
            twoLabel.setTextColor(getResources().getColor(R.color.color_878787));
        } else if (selectIndex == TWO_LABEL) {
            oneLabel.setTextColor(getResources().getColor(R.color.color_878787));
            twoLabel.setTextColor(getResources().getColor(R.color.colorPrimary));
        } else {
            oneLabel.setTextColor(getResources().getColor(R.color.color_878787));
            twoLabel.setTextColor(getResources().getColor(R.color.color_878787));
        }

    }
    // 展示UI

    private void resovleShowUI(View popRootView) {

        // 1级 recyclerview
        RecyclerView oneLabelRecyclerView = (RecyclerView) popRootView.findViewById(R.id.label1_recycler);
        // 2级 recyclerview
        RecyclerView label2LeftRecycler = (RecyclerView) popRootView.findViewById(R.id.label2_left_recycler);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());


        RelativeLayout oneLevelVp, twoLevelVp;
        oneLevelVp = popRootView.findViewById(R.id.one_level_vp);
        twoLevelVp = popRootView.findViewById(R.id.two_level_vp);
        rightLabel1 = popRootView.findViewById(R.id.right_label_1);
        rightLabel2 = popRootView.findViewById(R.id.right_label_2);
        btnAll = popRootView.findViewById(R.id.all_btn);
        isBtnAllSelected();

        setTabTitle();
        // 一级标签UI
        if (selectIndex == ONE_LEBAL) {
            oneLevelVp.setVisibility(VISIBLE);
            twoLevelVp.setVisibility(GONE);

            oneLabelRecyclerView.setLayoutManager(layoutManager);
            // 一级标签 adapter
            labelOneRecyclerViewAdapter = new LabelOneRecyclerViewAdapter(getContext());
            labelOneRecyclerViewAdapter.setOnItemClick(new LabelOneRecyclerViewAdapter.onItemClick() {
                @Override
                public void onItemClick(VipShareFirstLebalBean bean) {
                    if (chooseLebalListener != null) {
                        chooseLebalListener.selectFirstLabel(bean);
                        if (bean != null && bean.getTxt() != null)
                            selectTabTitle[0] = bean.getTxt();
                        for (VipShareFirstLebalBean tmp :
                                shareFirstLebalBeans) {
                            tmp.setSelected(tmp.equals(bean));
                        }

                        setShareFirstLebalData();
                    }
                    if (popRootView != null) {
                        popRootView.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                if (popWindow != null) popWindow.dismiss();

                            }
                        }, 300);
                    } else {
                        if (popWindow != null) popWindow.dismiss();
                    }


                }
            });
            oneLabelRecyclerView.setAdapter(labelOneRecyclerViewAdapter);
            setShareFirstLebalData();
        }
        //二级标签 ui

        btnAll.setOnClickListener(this);

        if (selectIndex == TWO_LABEL) {
            oneLevelVp.setVisibility(GONE);
            twoLevelVp.setVisibility(VISIBLE);

            reset = popRootView.findViewById(R.id.reset);
            confirm = popRootView.findViewById(R.id.confirm);
            reset.setOnClickListener(this);
            confirm.setOnClickListener(this);
            mFlowLayout = popRootView.findViewById(R.id.id_flowlayout);
            mFlowLayout2 = popRootView.findViewById(R.id.id_flowlayout2);

            // 二级标签 左侧
            label2LeftRecycler.setLayoutManager(layoutManager);
            labelTwoLeftRecyclerViewAdapter = new LabelTwoLeftRecyclerViewAdapter(getContext());
            labelTwoLeftRecyclerViewAdapter.setOnItemClick(new LabelTwoLeftRecyclerViewAdapter.onItemClick() {
                @Override
                public void onItemClick(VipShareSecondLeftLebalBean bean, int position) {

                    if (chooseLebalListener != null) {
                        if (tagAdapter1 != null) tagAdapter1.clearDatas();
                        if (tagAdapter2 != null) tagAdapter2.clearDatas();
                        refreshSecondLeftSelectData(position);
                        // 清空右标签文字
                        setRightLabelStr("", "");
                        chooseLebalListener.selectSecondLeftLabel(bean);
                    }
                }
            });
            label2LeftRecycler.setAdapter(labelTwoLeftRecyclerViewAdapter);

            // 刷新数据源
            setSecondLeftLabelData();

            // 2级标签 right adapter
            // 二级标签 刷新数据源
            // 第一部分
            setSecondRightLabel1Data();
            // 第二部分
            setSecondRightLabel2Data();
            setRightLabelsData();


        }

    }

    private void loadConfirmData() {
////        // 若有选择子标签
        if (confirmSelectedSecondRightLabel != null) {
            selectedSecondRightLabel = confirmSelectedSecondRightLabel;
            currentSelectedChildLeftLabelInedex = confirmSelectedSecondLeftLabelIndex;
            this.secondLeftLebalBeans.get(confirmSelectedSecondLeftLabelIndex).setChildSelected(true);

        }
        if (this.secondLeftLebalBeans != null && confirmSelectedSecondLeftLabelIndex < secondLeftLebalBeans.size()
                && secondLeftLebalBeans.get(confirmSelectedSecondLeftLabelIndex) != null) {
            // 没有点过重置按钮时 读取之前确认 的标签数据
            if (selectedSecondLeftLabelIndexInResetStatus == NO_SELECT) {
                currentSelectedSecondLeftLabelIndex = confirmSelectedSecondLeftLabelIndex;
                this.secondLeftLebalBeans.get(confirmSelectedSecondLeftLabelIndex).setSelected(true);
                if (confirmSelectedSecondLeftLabelIndex != 0 && secondLeftLebalBeans.get(0) != null)
                    secondLeftLebalBeans.get(0).setSelected(false);
            } else {
                resetStatus = true;
                if (currentSelectedSecondLeftLabelIndex < secondLeftLebalBeans.size()
                        && secondLeftLebalBeans.get(currentSelectedSecondLeftLabelIndex) != null)
                    secondLeftLebalBeans.get(currentSelectedSecondLeftLabelIndex).setSelected(false);
                currentSelectedSecondLeftLabelIndex = selectedSecondLeftLabelIndexInResetStatus;
            }

            if (chooseLebalListener != null)
                chooseLebalListener.selectSecondLeftLabel(secondLeftLebalBeans.get(currentSelectedSecondLeftLabelIndex));

        }

    }

    public void setRightLabelStr(String rightLabelStr1, String rightLabelStr2) {
        this.rightLabelStr1 = rightLabelStr1;
        this.rightLabelStr2 = rightLabelStr2;
        setRightLabelsData();
    }

    private void setRightLabelsData() {

        if (rightLabelStr1 != null && rightLabel1 != null) {
            if (!rightLabelStr1.equals("")) {
                rightLabel1.setVisibility(VISIBLE);
                rightLabel1.setText(rightLabelStr1);
            } else {
                rightLabel1.setVisibility(GONE);

            }
        }
        if (rightLabelStr2 != null && rightLabel2 != null) {

            rightLabel2.setText(rightLabelStr2);
        }
    }

    private VipShareSecondRightLebalBean getVipShareSecondRightLebalBean(Set<Integer> selectPosSet, int secondLabelIndex) {
        VipShareSecondRightLebalBean temp = null;
        for (Integer i : selectPosSet) {
            if (secondLabelIndex == SECOND_LABEL_ONE)
                temp = secondRightLebalBeans1.get(i);
            if (secondLabelIndex == SECOND_LABEL_TWO)
                temp = secondRightLebalBeans2.get(i);
        }
        return temp;
    }


    @NotNull
    private TagAdapter getTagAdapter(TagFlowLayout mFlowLayout, String[] mVals) {
        return new TagAdapter<String>(mVals) {

            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.level2_tag_tv,
                        mFlowLayout, false);
                tv.setText(s);
                return tv;
            }
        };
    }

    /* *==========数据源 处理=====================*/
    // 第一个标签
    // 一级标签 设置数据源
    public void setShareFirstLebalBeans(List<VipShareFirstLebalBean> shareFirstLebalBeans) {
        this.shareFirstLebalBeans = shareFirstLebalBeans;
        setShareFirstLebalData();
    }

    // 刷新数据 一级标签
    public void setShareFirstLebalData() {
        if (this.shareFirstLebalBeans != null && labelOneRecyclerViewAdapter != null) {
            labelOneRecyclerViewAdapter.setData(this.shareFirstLebalBeans);
        }
    }

    // 设置数据源
    // 第二个标签的 左侧标签
    public void setSecondLeftLebalBeans(List<VipShareSecondLeftLebalBean> secondLeftLebalBeans) {
        this.secondLeftLebalBeans = secondLeftLebalBeans;

        setSecondLeftLabelData();
    }
    // 刷新数据源 二级标签 左侧

    private void setSecondLeftLabelData() {
        if (secondLeftLebalBeans != null && labelTwoLeftRecyclerViewAdapter != null) {
            labelTwoLeftRecyclerViewAdapter.setData(this.secondLeftLebalBeans);
        }
    }

    // 点击 二级标签 右侧标签 ，刷新左侧标签 selected 数据
    private void refreshSecondLeftSelectData(int currentSelectedSecondLeftLabelIndex) {
        if (resetStatus) {
            revertResetStatus();
            setSecondLeftLabelData();
        }

        if (currentSelectedSecondLeftLabelIndex != this.currentSelectedSecondLeftLabelIndex) {
            lastSelectedSecondLeftLabelIndex = this.currentSelectedSecondLeftLabelIndex;

            if (lastSelectedSecondLeftLabelIndex != -1 && lastSelectedSecondLeftLabelIndex < secondLeftLebalBeans.size()
                    && secondLeftLebalBeans.get(lastSelectedSecondLeftLabelIndex) != null) {
                secondLeftLebalBeans.get(lastSelectedSecondLeftLabelIndex).setSelected(false);
            }
            if (currentSelectedSecondLeftLabelIndex != -1 &&
                    currentSelectedSecondLeftLabelIndex < secondLeftLebalBeans.size()
                    && secondLeftLebalBeans.get(currentSelectedSecondLeftLabelIndex) != null) {
                secondLeftLebalBeans.get(currentSelectedSecondLeftLabelIndex).setSelected(true);
            }
            setSecondLeftLabelData();
            this.currentSelectedSecondLeftLabelIndex = currentSelectedSecondLeftLabelIndex;


        }
    }

    private void revertResetStatus() {

        resetStatus = false;
        if (this.currentSelectedSecondLeftLabelIndex < secondLeftLebalBeans.size()
                && secondLeftLebalBeans.get(this.currentSelectedSecondLeftLabelIndex) != null)
            secondLeftLebalBeans.get(this.currentSelectedSecondLeftLabelIndex).setSelected(true);

    }

    // 二级 右侧标签 选中后刷新 左侧的 标签
    public void refreshSecondLeftChildSelectData(int currentSelectedChildLeftLabelInedex) {
        if (resetStatus)
            revertResetStatus();
        if (currentSelectedChildLeftLabelInedex != this.currentSelectedChildLeftLabelInedex) {
            lastSelectedChildLeftLabelInedex = this.currentSelectedChildLeftLabelInedex;

            if (lastSelectedChildLeftLabelInedex != -1 && lastSelectedChildLeftLabelInedex < secondLeftLebalBeans.size()
                    && secondLeftLebalBeans.get(lastSelectedChildLeftLabelInedex) != null) {
                secondLeftLebalBeans.get(lastSelectedChildLeftLabelInedex).setChildSelected(false);
            }
            this.currentSelectedChildLeftLabelInedex = currentSelectedChildLeftLabelInedex;

            if (currentSelectedChildLeftLabelInedex != -1 &&
                    currentSelectedChildLeftLabelInedex < secondLeftLebalBeans.size()
                    && secondLeftLebalBeans.get(currentSelectedChildLeftLabelInedex) != null) {
                secondLeftLebalBeans.get(currentSelectedChildLeftLabelInedex).setChildSelected(true);
            }
            setSecondLeftLabelData();

        }


    }

    // 设置 数据源
    public void setSecondRightLebalBeansAll(List<VipShareSecondRightLebalBean> secondRightLebalBeans1,
                                            List<VipShareSecondRightLebalBean> secondRightLebalBeans2) {

        if (tagAdapter2 != null) tagAdapter2.clearDatas();
        if (tagAdapter1 != null) tagAdapter1.clearDatas();
        setSecondRightLebalBeans1(secondRightLebalBeans1);
        setSecondRightLebalBeans2(secondRightLebalBeans2);
    }
    // 第二个标签的 右侧（2级）标签 （第一部分）


    public void setSecondRightLebalBeans1(List<VipShareSecondRightLebalBean> secondRightLebalBeans1) {

        this.secondRightLebalBeans1 = secondRightLebalBeans1;
        setSecondRightLabel1Data();


    }
    // 刷新 数据源 第二个标签的 右侧（2级）标签 （第一部分）

    public void setSecondRightLabel1Data() {
        if (this.secondRightLebalBeans1 == null || this.secondRightLebalBeans1.size() == 0) {
            return;
        }
        isBtnAllSelected();
        String[] mVals = new String[this.secondRightLebalBeans1.size()];
        int selectIndex = -1;

        for (int i = 0; i < this.secondRightLebalBeans1.size(); i++) {

            selectIndex = getSelectIndex(selectIndex, i, this.secondRightLebalBeans1);
            mVals[i] = this.secondRightLebalBeans1.get(i).getTxt();
        }

        if (mFlowLayout != null) {
            tagAdapter1 = getTagAdapter(mFlowLayout, mVals);
            mFlowLayout.setAdapter(tagAdapter1);
            isSelectedChildLabel(mVals, selectIndex, tagAdapter1);

            mFlowLayout.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
                @Override
                public void onSelected(Set<Integer> selectPosSet) {
                    if (selectedSecondRightLabel != null &&
                            selectedSecondRightLabel.getTxt().equals(SELECT_ALL)) {
                        btnAllUI(false);
                    }
                    if (selectPosSet.size() == 0) {
                        selectedSecondRightLabel = null;
                        refreshSecondLeftChildSelectData(-1);


                    } else {
                        refreshSecondLeftChildSelectData(currentSelectedSecondLeftLabelIndex);

                        selectedSecondRightLabel = getVipShareSecondRightLebalBean(selectPosSet, SECOND_LABEL_ONE);
                    }

                    if (tagAdapter2 != null) {
                        tagAdapter2.clearSelectedList();
                    }


                }
            });
        }
    }

    //判断是否有选中的 二级 右侧 标签
    private int getSelectIndex(int selectIndex, int i, List<VipShareSecondRightLebalBean> secondRightLebalBeans1) {
        if (this.selectedSecondRightLabel != null &&
                selectedSecondRightLabel.getUniId()
                        .equals(secondRightLebalBeans1.get(i).getUniId())) {
            selectIndex = i;
        }
        return selectIndex;
    }

    // 判断子标签是否被选中过
    private void isSelectedChildLabel(String[] mVals, int selectIndex, TagAdapter tagAdapter2) {
        // 判断子标签是否被选中过
        if (selectIndex != -1 && currentSelectedChildLeftLabelInedex == currentSelectedChildLeftLabelInedex
                && selectIndex < mVals.length) {
            Set set = new HashSet();
            set.add(selectIndex);
            tagAdapter2.setSelectedList(set);
        }
    }


    // 设置数据源 第二个标签的 右侧（2级）标签 （第二部分）
    public void setSecondRightLebalBeans2(List<VipShareSecondRightLebalBean> secondRightLebalBeans2) {

        this.secondRightLebalBeans2 = secondRightLebalBeans2;
        setSecondRightLabel2Data();

    }
    // 刷新 数据第二个标签的 右侧（2级）标签 （第二部分）

    private void setSecondRightLabel2Data() {
        if (this.secondRightLebalBeans2 == null || this.secondRightLebalBeans2.size() == 0) {
            return;
        }
        isBtnAllSelected();
        String[] mVals = new String[this.secondRightLebalBeans2.size()];
        int selectIndex = -1;
        for (int i = 0; i < this.secondRightLebalBeans2.size(); i++) {
            selectIndex = getSelectIndex(selectIndex, i, this.secondRightLebalBeans2);
            mVals[i] = this.secondRightLebalBeans2.get(i).getTxt();
        }
        if (mFlowLayout2 != null) {

            tagAdapter2 = getTagAdapter(mFlowLayout2, mVals);
            isSelectedChildLabel(mVals, selectIndex, tagAdapter2);

            mFlowLayout2.setAdapter(tagAdapter2);
            mFlowLayout2.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
                @Override
                public void onSelected(Set<Integer> selectPosSet) {
                    if (selectedSecondRightLabel != null && selectedSecondRightLabel.getTxt().equals(SELECT_ALL)) {
                        btnAll.setBackgroundResource(R.drawable.level2_tag_normal_bg);
                        btnAll.setTextColor(Color.parseColor("#5b5b5b"));
                    }
                    if (selectPosSet.size() == 0) {
                        selectedSecondRightLabel = null;
                        refreshSecondLeftChildSelectData(-1);

                    } else {
                        refreshSecondLeftChildSelectData(currentSelectedSecondLeftLabelIndex);
                        selectedSecondRightLabel = getVipShareSecondRightLebalBean(selectPosSet, SECOND_LABEL_TWO);
                    }

                    if (tagAdapter1 != null) tagAdapter1.clearSelectedList();
                }
            });
        }
    }

    // 全部按钮是否被选中
    private void isBtnAllSelected() {
        if (selectedSecondRightLabel != null && selectedSecondRightLabel.getUniId().equals(currentSelectedSecondLeftLabelIndex + SELECT_ALL)) {
            btnAllUI(true);
        } else {
            btnAllUI(false);

        }
    }

    private void btnAllUI(boolean b) {
        if (btnAll == null) return;
        if (b) {
            btnAll.setTag(b);
            btnAll.setBackgroundResource(R.drawable.level_2_tag_checked_bg);
            btnAll.setTextColor(Color.parseColor("#ffffff"));
        } else {
            btnAll.setBackgroundResource(R.drawable.level2_tag_normal_bg);
            btnAll.setTextColor(Color.parseColor("#5b5b5b"));
            btnAll.setTag(b);

        }

    }

    public void setChooseLebalListener(ChooseLebalListener chooseLebalListener) {
        this.chooseLebalListener = chooseLebalListener;
    }

    public interface ChooseLebalListener {
        // 选择的 第一级标签
        void selectFirstLabel(VipShareFirstLebalBean firstLebalBean);
        // 选择的 第二级标签 左侧

        void selectSecondLeftLabel(VipShareSecondLeftLebalBean secondLeftLebalBean);

        // 确认的 二级标签
        void confirmSecondRightLabels(VipShareSecondLeftLebalBean secondLeftLebalBean, VipShareSecondRightLebalBean secondRightLebalBean);

    }


}



